package com.example.madman.yuejian.Adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.madman.yuejian.Bean.ClubBean;
import com.example.madman.yuejian.ClubActivity;
import com.example.madman.yuejian.Fragment.ClubListFragment;
import com.example.madman.yuejian.Listener.RetrofitListener;
import com.example.madman.yuejian.R;

import java.util.List;


public class ClubListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private  List<ClubBean> list;
    private Context context;
    private LayoutInflater inflater;
    private ClubListFragment clubListFragment;
    private  RetrofitListener mListener;

    public ClubListAdapter(Context context)
    {
        this.inflater = LayoutInflater.from(context);
        this.context=context;
    }

    public void setList(List<ClubBean> l){
        list=l;
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = inflater.inflate(R.layout.fragment_clublist,parent,false);
        RecyclerView.ViewHolder vh = new ViewHolder(v);
        return vh;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        ClubBean bean = list.get(position);
        if(bean==null) return;
        final ViewHolder viewHolder= (ViewHolder) holder;
        viewHolder.text_club.setText(bean.getClubname());
       viewHolder.image_club.setImageBitmap(BitmapFactory.decodeResource(
                context.getResources(), R.drawable.club));
     /*   Picasso.with(context)
                .load("http://172.19.91.105/yuejian/public/static/images"+bean.getClubpic())
                .placeholder(R.drawable.club)
                .priority(Picasso.Priority.HIGH)
                .into(viewHolder.image_club);*/
        Glide.with(context)
                .load("http://192.168.155.1/yuejian/public/static/assets/img/"+bean.getClubpic())
                . placeholder(R.drawable.club)
                .into(viewHolder.image_club);




        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            int position = viewHolder.getLayoutPosition();

                Intent intent;
                intent = new Intent(context,ClubActivity.class);
                intent.putExtra("clubid",list.get(position).getClubid());
                context.startActivity(intent);
            }
        });



    }




    @Override
    public int getItemCount() {
        return list.size();
    }
    @Override
    public long getItemId(int position) {
        return position;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder{
        TextView text_club;
        ImageView image_club;

        public ViewHolder(View itemView) {
            super(itemView);
           text_club = (TextView)itemView.findViewById(R.id.text_club);
            image_club = itemView.findViewById(R.id.image_club);

        }
    }
}
